{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a20a2420",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 is not a quadratic residue modulo 7\n",
      "1 is a quadratic residue modulo 7\n",
      "2 is a quadratic residue modulo 7\n",
      "3 is not a quadratic residue modulo 7\n",
      "4 is a quadratic residue modulo 7\n",
      "5 is not a quadratic residue modulo 7\n",
      "6 is not a quadratic residue modulo 7\n"
     ]
    }
   ],
   "source": [
    "def legendre_symbol(a, p):\n",
    "    \"\"\"Calculate the Legendre symbol (a/p)\"\"\"\n",
    "    legendre = pow(a, (p - 1) // 2, p)\n",
    "    return -1 if legendre == p - 1 else legendre\n",
    "\n",
    "def is_quadratic_residue(a, p):\n",
    "    \"\"\"Check if a is a quadratic residue modulo p\"\"\"\n",
    "    legendre = legendre_symbol(a, p)\n",
    "    return legendre == 1\n",
    "\n",
    "# Example\n",
    "p = 7\n",
    "for a in range(p):\n",
    "    if is_quadratic_residue(a, p):\n",
    "        print(f\"{a} is a quadratic residue modulo {p}\")\n",
    "    else:\n",
    "        print(f\"{a} is not a quadratic residue modulo {p}\")\n",
    "\n",
    "## Output Example        \n",
    "# 0 is not a quadratic residue modulo 7\n",
    "# 1 is a quadratic residue modulo 7\n",
    "# 2 is a quadratic residue modulo 7\n",
    "# 3 is not a quadratic residue modulo 7\n",
    "# 4 is a quadratic residue modulo 7\n",
    "# 5 is not a quadratic residue modulo 7\n",
    "# 6 is not a quadratic residue modulo 7\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3263e4d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
